/**
 * @author harry
 * @date 2022/3/6/17:01
 */

package com.serviceOrchestration.pojo;

import com.serviceOrchestration.util.StatusCode;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
public class ReturnData<T> implements Serializable {
    private static final long serialVersionUID = -6577705090185331257L;

    private int code;
    private String msg;
    private T data;

    public ReturnData(int code) {
        this.code = code;
    }

    public ReturnData(StatusCode sc) {
        this.setStatus(sc);
    }

    public static <T> ReturnData<T> ok() {
        return new ReturnData<>(StatusCode.OK);
    }

    public static <T> ReturnData<T> ok(T data) {
        ReturnData<T> ret = new ReturnData<>(StatusCode.OK);
        ret.setData(data);
        return ret;
    }

    public static <T> ReturnData<T> error(StatusCode sc) {
        return new ReturnData<>(sc);
    }

    public static <T> ReturnData<T> error(StatusCode sc, T data) {
        ReturnData<T> ret = new ReturnData<>(sc);
        ret.setData(data);
        return ret;
    }

    public static <T> ReturnData<T> error(int code, String msg) {
        ReturnData<T> ret = new ReturnData<>(code);
        ret.setMsg(msg);
        return ret;
    }

    private void setStatus(StatusCode sc) {
        this.code = sc.value;
        this.msg = sc.information;
    }
}
